package com.pds.web.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.web.monitor.domain.PdsMonitorTaskSampling;
import com.pds.web.monitor.mapper.PdsMonitorTaskSamplingMapper;
import com.pds.web.monitor.service.IPdsMonitorTaskSamplingService;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Service
public class PdsMonitorTaskSamplingServiceImpl extends ServiceImpl<PdsMonitorTaskSamplingMapper, PdsMonitorTaskSampling> implements IPdsMonitorTaskSamplingService {

    @Resource
    private PdsMonitorTaskSamplingMapper pdsMonitorTaskSamplingMapper;

    /**
     * 更新
     *
     * @param pdsMonitorTaskSampling
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean update(PdsMonitorTaskSampling pdsMonitorTaskSampling) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsMonitorTaskSampling.setUpdateTime(date);
        return pdsMonitorTaskSamplingMapper.updateById(pdsMonitorTaskSampling) > 0;
    }

    /**
     * 查询
     *
     * @param pdsMonitorTaskSampling
     * @return
     */
    @Override
    public PdsMonitorTaskSampling selectSampling(PdsMonitorTaskSampling pdsMonitorTaskSampling) {
        QueryWrapper<PdsMonitorTaskSampling> wrapper = new QueryWrapper<>();

        Long taskId = pdsMonitorTaskSampling.getTaskId();
        Long organizationId = null;

        if (!Objects.isNull(taskId)) {
            wrapper.eq("task_id", taskId);
        }
        if (!Objects.isNull(organizationId)) {
            wrapper.eq("organization_id", organizationId);
        }

        return pdsMonitorTaskSamplingMapper.selectOne(wrapper);
    }
}
